<template>
  <el-dialog title="分配角色" :visible.sync="roleAttrShow">
    <el-checkbox-group v-model="userRoles">
      <el-checkbox :label="item.id" v-for="item in checkList" :key="item.id">{{item.name }}</el-checkbox>
    </el-checkbox-group>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="btnSub">确 定</el-button>
      <el-button @click="bntCel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      checkList: [],
      userRoles: []
    };
  },
  props: {
    roleAttrShow: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String
    }
  },
  watch: {
    userId: {
      handler(nval, oval) {
        this.getRoleList();
      }
    }
  },
  methods: {
    async getRoleList() {
      try {
        let { rows } = await this.$api.role.getRoleList();
        this.checkList = rows;
      } catch (error) {}
    },
    async getRolesById(userId) {
      try {
        let { roleIds } = await this.$api.employee.getEmployeeById(userId);
        this.userRoles = roleIds;
      } catch (error) {}
    },
    async btnSub() {
      try {
        await this.$api.role.assignRoles({
          id: this.userId,
          roleIds: this.userRoles
        });
        this.$message.success("分配成功!");
      } catch (error) {
        this.$message.error(error);
      }
      this.$emit("update:roleAttrShow", false);
    },
    bntCel() {
      this.checkedList = [];
      this.userRoles = [];
      this.$emit("update:roleAttrShow", false);
    }
  }
};
</script>

<style>
</style>